import xml.etree.ElementTree as ET
import config


def xmlToList(file):
    # 解析 XML 文件
    tree = ET.parse(f'D:\\文档\\DLMU\\ITC2019\\{file}.xml')
    root = tree.getroot()

    # 处理 选课表 数据
    room_list = []
    room_dist_list = []
    room_unavailable_list = []
    for room in root.findall('rooms/room'):
        # Insert into tbl_rooms
        room_id = room.get('id')  # Assuming the XML has an 'id' attribute for the room
        capacity = room.get('capacity')  # If capacity is provided
        room_list.append((room_id, capacity))

        for unavailable in room.findall('unavailable'):
            days = unavailable.get('days')
            start = unavailable.get('start')
            length = unavailable.get('length')
            weeks = unavailable.get('weeks')
            room_unavailable_list.append((room_id, days, start, length, weeks))


        # Insert into tbl_room_travel_times
        for travel in room.findall('travel'):
            to_room_id = travel.get('room')
            value = travel.get('value')
            room_dist_list.append((room_id, to_room_id, value))

    # 处理 选课表 数据
    s_c_list = []
    for student in root.findall('students/student'):
        id = student.get('id')

        # 遍历课程信息
        for travel_element in student.findall('course'):
            c_id = travel_element.get('id')
            s_c_list.append((id, c_id))


    # 处理课程信息表数据
    task_list = []
    class_room_penalty_list = []
    class_time_penalty_list = []
    for course in root.findall('courses/course'):
        id = course.get('id')

        for task in course.findall('config/subpart/class'):
            class_id = task.get('id')
            limit = task.get('limit')
            isRoom = True if task.get('room') is None else False
            task_list.append((id, class_id, limit, isRoom))
            for room in task.findall('room'):
                room_id = room.get('id')
                r_penalty = room.get('penalty')
                class_room_penalty_list.append((class_id, room_id, r_penalty))
            for time in task.findall('time'):
                days = time.get('days')
                start = time.get('start')
                length = time.get('length')
                weeks = time.get('weeks')
                t_penalty = time.get('penalty')
                class_time_penalty_list.append((class_id, days, start, length, weeks, t_penalty))

    return room_list, room_dist_list, room_unavailable_list, s_c_list, task_list,class_room_penalty_list,class_time_penalty_list
#  全部导入数据
if __name__ == '__main__':
    xmlToList(config.file[0])